package org.mparts.client.ui;

import java.util.List;

import org.mparts.client.content.Images;
import org.mparts.client.content.Strings;
import org.mparts.client.ui.IMainView;
import org.mparts.client.utils.Utils;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.ResizeComposite;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.sencha.gxt.core.client.XTemplates;
import com.sencha.gxt.widget.core.client.ContentPanel;

public class MainView extends ResizeComposite implements IsWidget, IMainView {
	
	@UiTemplate("MainView.ui.xml")
	public interface Binder extends UiBinder<Widget, MainView> {
	}
	
	@UiTemplate("MainViewTor.ui.xml")
	public interface BinderTor extends UiBinder<Widget, MainView> {
	}
	
	private IMainPresenter presenter;
	DataRenderer renderer = GWT.create(DataRenderer.class);
	
	@UiField
	ContentPanel panel;
	
	@UiField
	HTML text;
	
	private Binder uiBinder;
	private BinderTor uiBinderTor;
	
	private Widget root;

	@Inject
	public MainView(Images images, Strings strings, Binder uiBinder, BinderTor uiBinderTor) {
		Log.debug("create MainView...");
		this.uiBinder = uiBinder;
		this.uiBinderTor = uiBinderTor;
	}

	@Override
	public void setPresenter(IMainPresenter presenter) {
		this.presenter = presenter;		
	}


	public interface DataRenderer extends XTemplates {
		@XTemplate("<p>{title}<br><ul>" + 
					"<tpl for='data'> <li>{.}</li></tpl></ul></p>")
		public SafeHtml render(String title, List<String> data);

	}
		 
	@Override
	public Widget asWidget() {
		Log.debug("MainView.asWidget() ...");
		if ( root == null ) {
			Log.debug("MainView:: create root widget ...");			
			root = Utils.getCurrentStoreId() == 2L ? uiBinderTor.createAndBindUi(this) : uiBinder.createAndBindUi(this);
		}
		return root;
	}

}
